import 'package:built_collection/built_collection.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:mobile_app.core/core/domain/entities/email_app.dart';
import 'package:mobile_app.core/core/presentation/helpers/show_dialogs.dart';
import 'package:mobile_app.core/core/presentation/localization/translation.dart';
import 'package:mobile_app.core/core/presentation/widgets/dialogs/app_action_dialog.dart';
import 'package:mobile_app.core/core/presentation/widgets/dialogs/app_alert_dialog.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/addition_to_collection_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/addition_to_learning_queue_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/editing_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/exclusion_from_learning_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/mark_as_completely_learned_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/mark_as_known_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/mistake_report_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/progress_reset_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/removal_from_learning_queue_card_action.dart';
import 'package:mobile_app.home.core/core/domain/entities/card_actions/return_to_learning_card_action.dart';
import 'package:mobile_app.home.core/core/presentation/localization/translation.dart';

mixin CardWlocMixin {
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // card action building
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // <editor-fold>
  AppActionDialogAction buildMistakeReportCardAction({
    required MistakeReportCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).mistakeReportCardActionTitle,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildProgressResetCardAction({
    required ProgressResetCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).progressResetCardActionTitle,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildMarkAsKnownCardAction({
    required MarkAsKnownCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).markAsKnownCardActionTitle,
      showPremiumBadge: !cardAction.permitted,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildMarkAsCompletelyLearnedCardAction({
    required MarkAsCompletelyLearnedCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).markAsCompletelyLearnedCardActionTitle,
      showPremiumBadge: !cardAction.permitted,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildAdditionToLearningQueueCardAction({
    required AdditionToLearningQueueCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).additionToLearningQueueCardActionTitle,
      showPremiumBadge: !cardAction.permitted,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildRemovalFromLearningQueueCardAction({
    required RemovalFromLearningQueueCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).removalFromLearningQueueCardActionTitle,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildReturnToLearningCardAction({
    required ReturnToLearningCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).returnToLearningCardActionTitle,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildAdditionToCustomCollectionCardAction({
    required AdditionToCustomCollectionCardAction cardAction,
    required BuildContext context,
  }) {
    final String title;

    if (cardAction.collectionInfos.length == 1) {
      final collectionInfo = cardAction.collectionInfos.first;
      final collectionTitle = collectionInfo.customCollection.title;
      final collectionFolderTitle = collectionInfo.collectionFolderTitle;

      title = HomeCoreTranslation.of(context).buildAdditionToSingleCollectionActionTitle(
        collectionTitle: collectionTitle,
        collectionFolderTitle: collectionFolderTitle,
      );
    } else {
      title = HomeCoreTranslation.of(context).additionToMultipleCollectionActionTitle;
    }

    return AppActionDialogAction(
      title: title,
      showPremiumBadge: cardAction.collectionInfos.length == 1 && !cardAction.permitted,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildEditingCardAction({
    required EditingCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).editingCardActionTitle,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  AppActionDialogAction buildExclusionFromLearningCardAction({
    required ExclusionFromLearningCardAction cardAction,
    required BuildContext context,
  }) {
    return AppActionDialogAction(
      title: HomeCoreTranslation.of(context).exclusionFromLearningCardActionTitle,
      showPremiumBadge: !cardAction.permitted,
      onPressed: () {
        Navigator.of(context).pop(cardAction);
      },
    );
  }

  // </editor-fold>

  void showMistakeReportDialog({
    required MistakeReportCardAction cardAction,
    required VoidCallback onReportThroughTelegram,
    required VoidCallback onReportByEmail,
    required BuildContext context,
  }) async {
    final mistakeReportByEmailCardActionTitle =
        HomeCoreTranslation.of(context).buildMistakeReportByEmailCardActionTitle(
      availableMailAppCount: cardAction.availableEmailApps.length,
    );

    await showAppActionDialog(
      context: context,
      builder: (context) {
        return AppActionDialog(
          actions: [
            AppActionDialogAction(
              title: HomeCoreTranslation.of(context).mistakeReportThroughTelegramCardActionTitle,
              isDefaultAction: true,
              onPressed: () {
                onReportThroughTelegram();
                Navigator.of(context).pop();
              },
            ),
            AppActionDialogAction(
              title: mistakeReportByEmailCardActionTitle,
              onPressed: () {
                onReportByEmail();
                Navigator.of(context).pop();
              },
            ),
          ],
          onCancelButtonPressed: Navigator.of(context).pop,
        );
      },
    );
  }

  void showMailAppChoosingDialog({
    required BuildContext context,
    required BuiltList<EmailApp> availableMailApps,
    required void Function(EmailApp mailApp) onEmailAppChosen,
  }) async {
    final sortedMailApps = availableMailApps.toList();

    sortedMailApps.sort((firstMailApp, secondMailApp) {
      final firstMailAppName = HomeCoreTranslation.of(context).getMailAppCardActionTitle(
        mailApp: firstMailApp,
      );

      final secondMailAppName = HomeCoreTranslation.of(context).getMailAppCardActionTitle(
        mailApp: secondMailApp,
      );

      return firstMailAppName.compareTo(secondMailAppName);
    });

    await showAppActionDialog(
      context: context,
      builder: (context) {
        return AppActionDialog(
          message: HomeCoreTranslation.of(context).mailAppChoosingDialogTitle,
          actions: sortedMailApps.map((mailApp) {
            final title = HomeCoreTranslation.of(context).getMailAppCardActionTitle(
              mailApp: mailApp,
            );

            return AppActionDialogAction(
              title: title,
              onPressed: () {
                onEmailAppChosen(mailApp);
                Navigator.of(context).pop();
              },
            );
          }).toList(),
          onCancelButtonPressed: Navigator.of(context).pop,
        );
      },
    );
  }

  void showAdditionToCollectionDialog({
    required BuildContext context,
    required AdditionToCustomCollectionCardAction cardAction,
    required void Function(String customCollectionId) onAddToCustomCollection,
  }) async {
    final customCollectionId = await showAppActionDialog<String>(
      context: context,
      builder: (context) {
        return AppActionDialog(
          actions: cardAction.collectionInfos.map((collectionInfo) {
            final collection = collectionInfo.customCollection;
            final collectionTitle = collection.title;
            final collectionFolderTitle = collectionInfo.collectionFolderTitle;

            return AppActionDialogAction(
              title: HomeCoreTranslation.of(context).buildCardCollectionActionTitle(
                collectionTitle: collectionTitle,
                collectionFolderTitle: collectionFolderTitle,
              ),
              showPremiumBadge: !collectionInfo.collectionPermitted,
              onPressed: () {
                Navigator.of(context).pop(collection.id);
              },
            );
          }).toList(),
          onCancelButtonPressed: Navigator.of(context).pop,
        );
      },
    );

    if (customCollectionId != null) {
      onAddToCustomCollection(customCollectionId);
    }
  }

  void showPronunciationCautionDialog({
    required BuildContext context,
  }) {
    showAppAlertDialog(
      context: context,
      builder: (context) {
        return AppAlertDialog(
          title: HomeCoreTranslation.of(context).pronunciationCautionDialogTitle,
          message: HomeCoreTranslation.of(context).pronunciationCautionDialogMessage,
          actions: [
            AppAlertDialogAction(
              title: CoreTranslation.of(context).ok,
              onPressed: Navigator.of(context).pop,
            ),
          ],
        );
      },
    );
  }
}
